package main //声明 main 包，表明当前是一个可执行程序

import (
	"flag"
	"fmt"
	"os"
	"path/filepath"
	"strings"

	"gitee.com/smewl13/learn/loadcount"
	"gitee.com/smewl13/learn/utils"
	// "gitee.com/smewl13/learn/practise"
)

func main() {
	var cmd string
	var startTime string
	var endTime string
	var confFile string
	var sqlFile string

	procName := filepath.Base(os.Args[0])
	procRoot, _ := filepath.Abs(filepath.Dir(os.Args[0]))

	flag.StringVar(&confFile, "c", fmt.Sprintf("%s/conf/%s.yaml", procRoot, procName), "配置文件，默认为程序目录下conf/{procname}.yaml")
	flag.StringVar(&sqlFile, "f", "", "查询的sql文件，默认为空")
	flag.StringVar(&startTime, "s", "00:00:00", "开始时间，默认为0点")
	flag.StringVar(&endTime, "e", "23:59:59", "结束时间，默认为23:59:59")

	flag.Parse()
	// fmt.Printf("conf=%v\nstarttime=%v\nendtime=%v\n", confFile, startTime, endTime)

	startTime, err := utils.FormatTime(startTime)
	if err != nil {
		utils.Error.Fatalf("开始时间错误: %s\n", err)
	}
	endTime, err = utils.FormatTime(endTime)
	if err != nil {
		utils.Error.Fatalf("结束时间错误: %s\n", err)
	}

	if sqlFile != "" {
		_, err := os.Stat(sqlFile)
		if err != nil {
			sqlFile = ""
		}
	}

	if flag.NArg() > 0 {
		cmd = strings.ToLower(flag.Arg(0))
	} else {
		cmd = "query"
	}

	conf := utils.LoadConf(confFile)
	options := &loadcount.Options{
		StartTime: startTime,
		EndTime:   endTime,
		Cmd:       cmd,
		SqlFile:   sqlFile,
		Conf:      &conf,
	}
	loadcount.LoadCount(options)

	// courses.String01()
	// courses.Materail0801()
	// courses.Materail0802()
	// practise.Work0601()
	// practise.Work0602()
	// practise.Work0603()
	// practise.Work0604()
	// practise.Work0605()
	// courses.Materail0701()
	// courses.Materail0702()
	// courses.Materail0703()
	// courses.Materail0704()
	// courses.Materail0705()
	// courses.Materail0706()
	// courses.Materail0707()
	// courses.Materail0708()
	// practise.Work0701()
	// practise.Work0702()
	// practise.Work0703()
	// practise.Work0704()
	// practise.Work0705()
	// practise.Work0706()
	// practise.Work0707()
}

func IntContains(list []int, num int) bool {
	for _, v := range list {
		// fmt.Println(v)
		if num == v {
			return true
		}
	}
	return false
}
